package com.adan.auditlog.handler;

import com.adan.auditlog.entity.DBOperationLog;
import com.adan.auditlog.entity.HandlerParam;
import com.adan.auditlog.entity.SqlCommandType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 数据库实体更新处理器
 *
 * @see com.baomidou.mybatisplus.core.mapper.BaseMapper#updateById(Object)
 */
@Component
@Slf4j
public class AuditInsertEntityHandler extends AbstractAuditHandler {

	@Override
	public boolean matchMethod(String methodName) {
		return methodName.startsWith(SqlCommandType.INSERT_BY_ENTITY);
	}

	@Override
	public Object doHandle(HandlerParam param, DBOperationLog pLog) throws Throwable {
		Object[] args = param.getProceedingJoinPoint().getArgs();
		if (args != null && args.length > 0 && args[0] instanceof DBOperationLog) {
			return param.getProceedingJoinPoint().proceed();
		}
		// 执行sql
		Integer result = (Integer) param.getProceedingJoinPoint().proceed();
		if (result > 0) {
			String afterValue = afterOperate(pLog, args);
			pLog.setAfterValue(afterValue);
		}
		sqlLogMapper.insert(pLog);
		return result;
	}
}
